class Double
	attr_reader :first, :second
	attr_writer :first, :second
	def initialize(art1, art2)
		@first = art1
		@second = art2
	end
end

class DoubleSet
	def initialize(doubleArr)
		@doubleArray = doubleArr
	end
	def getBestMatch(artist)
# 	get the best match in the doubles for the corresponding artist
# 	1. Search for matching doubles
		arr = Array.new
		for i in @doubleArray
			if i.first == artist
				arr.push(i.second)
			end
			if i.second == artist
				arr.push(i.first)
			end
		end
		arr.sort
#		make a list of possible matches...
		list = Array.new
		nxt = nil
		cur = nil
		i = 0
		num = 0
		while i < (arr.size - 1)
			num = num + 1
			cur = arr[i]
			nxt = arr[i + 1]
			if cur != nxt 
				list.push(Double.new(cur, num))
				num = 0
			end
			i = i + 1
		end
#		Don't forget the last element
		list.push(Double.new(arr[arr.size - 1], num))

		list = randomize(list)
		biggest = getBiggest(list)
		return biggest.first
	end
	def randomize(list)
		list.each do |elem|
			num = rand(10) - 5
			elem.second += num
		end
		return list
	end
	def getBiggest(list)
		biggest = list[0]
		for i in list
			if i.second > biggest.second
				biggest = i
			end
		end
		return biggest
	end
end

def get_distinct(array)
	i = 0
	nxt = nil
	cur = nil
	doubles = Array.new
	while i < (array.length - 1)
		cur = array[i]
		nxt = array[i + 1]
		if cur != nxt
			doubles.push(Double.new(cur, nxt))
		end
		i = i + 1
	end
	doubles
end

array = Array.new
File.open("./test.txt").each do |line|
	spl = line.split(';')
	time = spl[0]
	artist = spl[1]
	artist = artist[0..(artist.size-2)]
	array.push(artist)
end

doubles = get_distinct(array)
set = DoubleSet.new(doubles)
puts set.getBestMatch(ARGV[0])

